﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace OnLineLogFileClient.Move
{
    class Program
    {
        static Dictionary<string, string> dic = new Dictionary<string, string>();
        static void Main(string[] args)
        {
            try
            {
                NewMethod(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.Read();
            }
        }

        private static void NewMethod(string[] args)
        {
            if (args != null)
            {
                foreach (var item in args)
                {
                    Console.WriteLine(item);
                }
            }
            //-s 需要启动的程序
            //-sarg 需要启动的程序的参数
            //-f 移动文件来自 
            //-t 移动到哪去
            //-nm 跳过文件移动逻辑
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-"))
                {
                    string val = null;
                    if ((i + 1) < args.Length)
                    {
                        val = args[i + 1];
                    }
                    dic.Add(args[i], val);
                }
            }
            if (!dic.ContainsKey("-nm"))
            {
                if (!dic.ContainsKey("-f") || !dic.ContainsKey("-t"))
                {
                    Console.WriteLine("缺少参数 -f or -t");
                    Console.Read();
                    return;
                }
                var form = dic["-f"];
                var to = dic["-t"];
                DirectoryInfo di = new DirectoryInfo(form);
                di.Refresh();
                var files = Directory.EnumerateFiles(form);
                foreach (var item in files)
                {
                    Console.WriteLine(item);
                }
                Thread.Sleep(3000);
                foreach (var item in files)
                {
                    var toF = Path.Combine(to, Path.GetFileName(item));
                    if (File.Exists(toF))
                        File.Delete(toF);
                    Console.WriteLine("-------------------------move:" + item);
                    Console.WriteLine("-------------------------toF:" + toF);
                    if (!File.Exists(item))
                    {
                        Console.WriteLine("---------------------file not found:" + item);
                    }
                    else
                    {
                        Console.WriteLine("-------------------file Exists:" + item);
                        //Console.WriteLine(File.ReadAllText(item));
                        try
                        {
                            File.Move(item, toF);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("--------------------" + ex.Message);
                        }
                    }

                    //File.Move(item, toF);

                }
            }
            string startArgs;
            dic.TryGetValue("-sarg", out startArgs);
            string start;
            dic.TryGetValue("-s", out start);

            if (start != null)
            {
                var fb = GetAssemblyPath();
                start = Path.Combine(fb, start);
                Console.WriteLine(start + " " + startArgs);
                Process.Start(start, startArgs);
                //Console.Read();
            }
        }
        private static string GetAssemblyPath()
        {
            string l = System.Reflection.Assembly.GetEntryAssembly().Location;

            FileInfo fi = new FileInfo(l);
            Console.WriteLine("==========" + fi.DirectoryName);
            return fi.DirectoryName;
        }
    }
}
